package com.hu.web.controller;

import com.hu.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class PayController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/pay/user/{id}") //浏览器来调用
    @HystrixCommand(fallbackMethod = "payFallbackMethod") //需要熔断的方法
    public User pay(@PathVariable("id") Long id){
//        String url = "http://localhost:1000/user/"+id; //有了负载之后改一下url (通过名字来找的)
        String url = "http://user-server/user/"+id;
        User user = restTemplate.getForObject(url, User.class);
        return user;
    }

    //托底方法（二手方案） 返回值、参数和需要熔断的方法一致  注意托底方法名
    public User payFallbackMethod(@PathVariable("id") Long id){
        return new User(-1L,"无此用户","用户服务不可用");
    }
}
